/* typehints:start */
import { Application } from "../application";
import { SaveGame } from "./savegame";
import { GameHUD } from "./huds/hud";
import { GameState } from "../states/game";
import { People } from "./people";
import { WorldMap } from "./maps/world_map";
import { Event } from "./game_event";
import { GameTask } from "./task/game_task";
import { Time } from "./time";
import { BackDrop } from "./backdrop";
/* typehints:end */

import { createLogger } from "../core/utils/logging";
import { matchArray, ObjectDelKey } from "../core/utils/utils";
import { BattleManager } from "./battle/battle_manager";
import { Signal } from "../core/utils/signal";

const logger = createLogger('game/root');

export class GameRoot{
  constructor(app){
    /** @type {Application} */
    this.app = app;

    /** @type {SaveGame} */
    this.savegame = null;

    /** @type {GameHUD} */
    this.hud = null;

    /** @type {GameState} */
    this.gameState = null;

    /** @type {WorldMap} */
    this.worldMap = null;

    /** @type {Event} */
    this.event = null;

    /** @type {GameTask} */
    this.task = null;

    /** @type {Time} */
    this.time = null;

    /** @type {BackDrop} */
    this.backdrop = null;

    /** @type {BattleManager} */
    this.battleMgr = null;

    // 玩家数据
    /** @type {People} */
    this.playerData = null;
    // 世界数据
    this.worldData = null;
    // 所有元素数据
    this.itemData = null;
    // 杂项数据，包括玩家背包及其他系统数据
    this.sundryData = null;
    // 主要人物索引
    /** @type {People} */
    this.mainPlayer = null;

    /**
     * 是否加载完毕
     */
    this.gameInitialized = false;
    /**
     * 是否播放游戏，世界中
     */
    this.gamePlay = true;
    /**
     * 战斗更新
     */
    this.battlePlay = false;

    this.signals = {
      aboutToDestruct: new Signal,
    }
  }

  /**
   * ---------   快捷调用  ----------
   */

  /**
   * 找到玩家队伍指定生灵
   * @param {string} _id 
   * @returns 
   */
  findPlayerPeople(_id){
    return matchArray(this.playerData.troops, {
      _id
    }, 'data');
  }

  /**
   * 保存游戏数据
   */
   savePlayerData(){
    this.savegame.saveData({
      playerData: {
        ...this.playerData,
        troops: this.playerData.troops.map(item => {
          return item.internalGetData();
        })
      }
    })
  }
  saveWorldData(){
    this.savegame.saveData({
      worldData: this.worldData,
    })
  }
  saveItemData(){
    this.savegame.saveData({
      itemData: this.itemData
    })
  }
  saveSundryData(){
    this.savegame.saveData({
      sundryData: this.sundryData
    })
  }

  // 调用信息signal
  message(...arg){
    this.hud.signals.showMessage.dispatch(...arg);
  }

  stopGame(){
    if(!this.gamePlay) return;
    logger.log('🌏 世界暂停');
    this.gamePlay = false;
  }

  playGame(){
    // 战斗中不可直接运行世界进程，需要使用stopBattle
    if(this.battlePlay || this.gamePlay) return;
    logger.log('🌏 世界运行');
    this.gamePlay = true;
  }

  playBattle(){
    this.battlePlay = true;
    this.stopGame();
  }

  stopBattle(){
    this.battlePlay = false;
    this.playGame();
  }

  destruct(){
    logger.log('destructing root');
    this.signals.aboutToDestruct.dispatch();

    this.reset();
  }

  reset(){
    if(this.signals){
      for(let i = 0; i < this.signals.length; i++){
        this.signals[i].removeAll();
      }
    }

    if(this.hud){
      this.hud.cleanup();
    }

    for(let prop in this){
      if(this.hasOwnProperty(prop)){
        delete this[prop];
      }
    }
  }
}